package tinking.java.operator;

/**
 * @author GaoFeng
 * @date 2019/12/25 9:29
 */
public class URShift {
    public static void main(String[] args) {

        // 无符号右移
        int i = -1;
        System.out.println(Integer.toBinaryString(i) + " => " + i);

        // 如果超过32位,会对位数取模
        System.out.println("-1 >> 32 => " + (-1 >>> 32));

        // 负数右移n位，即在将前n位1变成0，包括符号位 => 2^(32 - n) + 原数
        i >>>=10;
        System.out.println(Integer.toBinaryString(i) + " => " + i);
        System.out.println("2^22 - 1 => " + (pow(22) - 1));
        System.out.println("-1 >>> 32 => " + (-1 >>> 32));

        // 最后可能是第五问问
        // 当然这是无法推算的

        long l = -1;
        System.out.println(Long.toBinaryString(l) + " => " + l);
        l >>>= 10;
        System.out.println(Long.toBinaryString(l) + " => " + l);
        System.out.println("2^54 - 1 => " + (pow(54) - 1));

        short s = -1;
        System.out.println(Integer.toBinaryString(s) + " => " + s);

        // 右移的结果超出了范围，右移10位，还剩22个1，赋值给short，等于
        s >>>= 17;
        System.out.println(Integer.toBinaryString(s) + " s >>> 10 => " + s);


        byte b = -1;
        System.out.println(Integer.toBinaryString(b) + " => " + b);


        b >>>= 10;
        System.out.println(Integer.toBinaryString(b) + " b >>> 10 => " + b);

        b = -1;
        System.out.println(Integer.toBinaryString(b)  + " => " + b);
        // 每个人都会实现，，到一部分或者还是当然每个人
        System.out.println(Integer.toBinaryString(b >>> 10) + " => " + b);

    }

    private static long pow(int n) {

        long result = 1;

        for (int i = 0; i < n; i++) {
            result *= 2;
        }

        return result;
    }
}
